﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace System
{
    public static class SystempHelper
    {
        /// <summary>
        /// string To Int
        /// </summary>
        /// <param name="source">字符串g</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>System.Int32.</returns>
        public static int ToInt32(this string source, int defaultValue = 0)
        {
            int num = 0;
            int result;
            if (int.TryParse(source, out num))
            {
                result = num;
            }
            else
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// string 转换 Int64
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int64.</returns>
        public static long ToInt64(this string source, long defaultValue = 0L)
        {
            long num = 0L;
            long result;
            if (long.TryParse(source, out num))
            {
                result = num;
            }
            else
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// string 转换 Double
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Double.</returns>
        public static double ToDouble(this string source, double defaultValue = 0.0)
        {
            double num = 0.0;
            double result = defaultValue;
            if (double.TryParse(source, out num))
            {
                result = num;
            }
            return result;
        }

        /// <summary>
        /// To the float.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>System.Single.</returns>
        public static float ToFloat(this string source, float defaultValue = 0)
        {
            float num = 0;
            float result = defaultValue;
            if (float.TryParse(source, out num))
            {
                result = num;
            }
            return result;
        }

        /// <summary>
        /// string 转换 Decimal
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Decimal.</returns>
        public static decimal ToDecimal(this string source, decimal defaultValue = 0)
        {
            decimal num = defaultValue;
            decimal result;
            if (decimal.TryParse(source, out num))
            {
                result = num;
            }
            else
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// string 转换 Boolean
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        public static bool ToBool(this string source, bool defaultValue = false)
        {
            bool flag = false;
            bool result;
            if (bool.TryParse(source, out flag))
            {
                result = flag;
            }
            else
            {
                if (source == "0")
                    result = false;
                else if (source == "1")
                    result = true;
                else
                    result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// string 转换 2次幂总和
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static int ToPowInt(this string source, char spit = ',')
        {
            var tempInfos = source.Split(spit);
            int num = tempInfos.ToPowInt();
            return num;
        }

        /// <summary>
        /// string 转换 2次幂总和
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static int ToPowInt(this string[] source)
        {
            int num = 0;
            foreach (var item in source)
            {
                num += (int)Math.Pow(2, item.ToInt32());
            }
            return num;
        }

        /// <summary>
        /// string 转换 2次幂总和
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static int ToPowInt(this List<int> source)
        {
            int num = 0;
            foreach (var item in source)
            {
                num += (int)Math.Pow(2, item);
            }
            return num;
        }

        /// <summary>
        /// string 转换 2次幂总和
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static int ToPowInt(this int source)
        {
            int num = (int)Math.Pow(2, source);
            return num;
        }

        /// <summary>
        /// string集合转换带间隔的string
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static string ToStringSpit(this List<string> source, char spit = ',')
        {
            string str = "";
            foreach (var item in source)
            {
                str += item + spit;
            }
            str.TrimEnd(spit);
            return str;
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(this double FactSize)
        {
            string m_strSize = "";
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(this int FactSize)
        {
            string m_strSize = "";
            if (FactSize < 1024)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024 && FactSize < 1048576)
                m_strSize = (FactSize / 1024).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024 / 1024).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024 / 1024 / 1024).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(this long Size)
        {
            string m_strSize = "";
            long FactSize = 0;
            FactSize = Size;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// string 转换 DateTime
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>DateTime.</returns>
        public static DateTime ToDateTime(this string source, DateTime defaultValue = default(DateTime))
        {
            DateTime now = DateTime.Now;
            DateTime result;
            if (DateTime.TryParse(source, out now))
            {
                result = now;
            }
            else
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// string 是否为 null 或 empty
        /// </summary>
        /// <param name="source">字符串</param>
        /// <param name="ignoreWhiteSpace"></param>
        /// <returns>bool</returns>
        public static bool IsNullOrWhiteSpace(this string source)
        {
            bool result;
            result = source == null || string.IsNullOrWhiteSpace(source);
            return result;
        }

        /// <summary>
        /// 深拷贝
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <returns>T.</returns>
        public static T CloneJson<T>(this T source) where T : class, new()
        {
            if (source == null)
            {
                return null;
            }
            var s = JsonConvert.SerializeObject(source);
            return (T)JsonConvert.DeserializeObject(s, source.GetType());
        }

        #region 正则

        /// <summary>
        /// string 是否为 数字
        /// </summary>
        /// <param name="source">字符串</param>
        /// <returns>bool</returns>
        public static bool IsRegexNumber(this string source)
        {
            bool result = Regex.Match(source, Consts.RegexNumber).Success;
            return result;
        }

        /// <summary>
        /// string 是否为 字母(大小写)
        /// </summary>
        /// <param name="source">字符串</param>
        /// <returns>bool</returns>
        public static bool IsRegexString(this string source)
        {
            bool result = Regex.Match(source, Consts.RegexString).Success;
            return result;
        }
        #endregion
    }

}
